package servlet;

import java.io.*;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mgr.ClienteManager;
import to.ClienteTO;

import mgr.TipoDocumentoManager;

@SuppressWarnings("serial")
public class BuscarClienteAction extends HttpServlet {
    
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        procesar(req, resp);
    }
    
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        procesar(req, resp);
    }

    private void procesar(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String cCliente = req.getParameter("cCliente");
        String dNombres = req.getParameter("dNombres");
        String cTipoDocumento = req.getParameter("cTipoDocumento");
        String nDocumento = req.getParameter("nDocumento");
        
        try {
            List<ClienteTO> listadoClientes;
            ClienteTO cli_to = new ClienteTO(cCliente, dNombres, dNombres, dNombres, "", cTipoDocumento, nDocumento, "", "", "", 0, "", "", "");
            ClienteManager clientes = new ClienteManager();
            listadoClientes = clientes.filtraCliente(cli_to);
            
            List listadoDocumentos;
            TipoDocumentoManager tipos = new TipoDocumentoManager();
            listadoDocumentos = tipos.filtraDocumentos();
            
            HttpSession sesion = req.getSession();
            sesion.setAttribute("listadoClientes", listadoClientes);
            sesion.setAttribute("listadoDocumentos", listadoDocumentos);
            
            getServletContext().getRequestDispatcher("/Listado_Clientes.jsp").forward(req, resp);
            
        }
        catch (Exception e) {
            PrintWriter out = resp.getWriter();
            out.println("<HTML>");
            out.println("<HEAD>");
            out.println("</HEAD>");
            out.println("<BODY>");

            String msg = (String) e.getMessage();
            out.println("<P>" + msg + "</P>");

            out.println("</BODY>");
            out.println("</HTML>");
	}
    }
}